﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 问卷类别管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PaperClsController : AdminBaseController
    {
        //
        // GET: /Admin/PaperCls/
        /// <summary>
        /// 问卷类别管理主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_PaperCls", Order = 1)]
        [LogPageActionFilter("问卷类别管理", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 树
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult PaperClsTree() 
        {
            TreeModelList treeModelList = new TreeModelList();
            BLL.PaperCls paperClsBll = new BLL.PaperCls();
            List<Model.PaperCls> paperClsAllList = paperClsBll.GetModelList(GlobalConstant.ENTITYNO);

            TreeItemModel treeParentItemModel = new TreeItemModel(null)
            {
                Id = "0",
                Name = "问卷类别列表",
                ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

            };

            List<Model.PaperCls> paperClsList = paperClsAllList.Where(c => c.FPaperClsNo != null).ToList();

            if (paperClsList.Count > 0)
            {
                foreach (Model.PaperCls paperCls in paperClsList)
                {
                    TreeItemModel treeSonItemModel = new TreeItemModel(treeParentItemModel)
                    {
                        Id = paperCls.FPaperClsNo,
                        Name = paperCls.FPaperClsName,
                        ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

                    };

                    treeParentItemModel.Id = "%!^" + treeParentItemModel.Id;

                }
            }
            treeModelList.Add(treeParentItemModel);

            return View(treeModelList);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="paperClsNo">编号</param>
        /// <returns>视图</returns>
        public ActionResult PaperClsContent(string paperClsNo) 
        {
            Model.PaperCls paperClsModel = new Model.PaperCls();

            ViewData["plcls"] = true;
            ViewData["jsAlert"] = "";
            if (string.IsNullOrEmpty(paperClsNo))
            {
               
                ViewData["plcls"] = true;
                paperClsNo = null;
                ViewData["type"] = "ADD";

            }
            else 
            {
                ViewData["plcls"] = false;
                ViewData["type"] = "Edit";
                paperClsNo = Fetch.estimateString(paperClsNo, CheckGetEnum.Safety);
                BLL.PaperCls paperClsBll = new BLL.PaperCls();
                paperClsModel = paperClsBll.GetModel(paperClsNo, GlobalConstant.ENTITYNO);
                if (paperClsModel == null) {
                    paperClsModel = new Model.PaperCls();
                }

            }
            return View(paperClsModel);
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="paperClsModel">实体数据</param>
        /// <returns>视图</returns>
        public ActionResult Save(Model.PaperCls paperClsModel) 
        {
            BLL.PaperCls paperBll = new BLL.PaperCls();
            ViewData["plcls"] = true;
            string paperClsNo = Fetch.Post("FPaperClsNo").Trim();
            string paperClsName = Fetch.Post("FPaperClsName").Trim();
            string type = Fetch.Post("hdtype");
            if (!"".Equals(paperClsNo) && !"".Equals(paperClsName))
            {
                if (type == "ADD")
                {
                    if (!paperBll.Exists(paperClsNo, GlobalConstant.ENTITYNO))
                    {
                        paperClsModel.FPaperClsNo = paperClsNo;
                        paperClsModel.FPaperClsName = paperClsName;
                        paperClsModel.FEntityNo = GlobalConstant.ENTITYNO;
                        paperBll.Add(paperClsModel);

                        Response.Write("<script>alert('操作成功');parent.window.location.href = '" + Url.Action("Index", "PaperCls") + "'</script>");
                        Response.Flush();
                        Response.End();

                    }
                    else
                    {
                        ViewData["jsAlert"] = "对不起，该问卷类别编号已存在！";
                    }
                }
                else if (type == "Edit")
                {
                    paperClsModel.FPaperClsNo = paperClsNo;
                    paperClsModel.FPaperClsName = paperClsName;
                    paperClsModel.FEntityNo = GlobalConstant.ENTITYNO;
                    paperBll.Update(paperClsModel);
                    Response.Write("<script>alert('操作成功');parent.window.location.href = '" + Url.Action("Index", "PaperCls") + "'</script>");
                    Response.Flush();
                    Response.End();

                }

            }
            else {
                ViewData["jsAlert"] = "对不起，输入项不能为空！";
            }


            return View("PaperClsContent", paperClsModel);      
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fPaperCls">编号</param>
        /// <param name="fEntityNo">公司编号</param>
        /// <returns>视图</returns>
        public string Delete(string fpaperClsNo, string fEntityNo) 
        {
            string str = string.Empty;
            BLL.PaperCls paperClsBll = new BLL.PaperCls();
            BLL.Paper paperBll = new BLL.Paper();
            try
            {
                if (paperBll.Exists(fpaperClsNo, GlobalConstant.ENTITYNO))
                {
                    str = "exists";
                }
                else
                {
                    paperClsBll.Delete(fpaperClsNo, GlobalConstant.ENTITYNO);
                    str = "ok";
                }
            }
            catch (Exception)
            {
                str = "error";
            }

            return str;

        }


    }
}
